package com.iwomy.secureplat.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.core.constant.CacheConstants;
import com.iwomy.secureplat.platform.entity.AlarmDeviceRulesEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.entity.DeviceEventReportEntity;
import com.iwomy.secureplat.platform.mapper.AlarmDeviceRulesMapper;
import com.iwomy.secureplat.platform.service.AlarmDeviceRulesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报警规则与设备管理表
 *
 * @author pig
 * @date 2024-02-19 14:22:24
 */
@Service
@Slf4j
public class AlarmDeviceRulesServiceImpl extends ServiceImpl<AlarmDeviceRulesMapper, AlarmDeviceRulesEntity> implements AlarmDeviceRulesService {
	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.EQUIPMENT_ALARM_RULES,allEntries = true)
	public void addDeviceRulesByRules(Long rulesId, List<BasicDeviceEntity> deviceList) {
		//删除之前的记录
		remove(Wrappers.<AlarmDeviceRulesEntity>lambdaQuery().eq(AlarmDeviceRulesEntity::getAlarmRulesId, rulesId));
		//新增新的
		List<AlarmDeviceRulesEntity> alarmDeviceRulesList = deviceList.stream().map(x -> {
			AlarmDeviceRulesEntity alarmDeviceRulesEntity = new AlarmDeviceRulesEntity();
			alarmDeviceRulesEntity.setAlarmRulesId(rulesId);
			alarmDeviceRulesEntity.setImei(x.getImei());
			alarmDeviceRulesEntity.setDeviceModelId(x.getDeviceModelId());
			alarmDeviceRulesEntity.setDeviceModelName(x.getDeviceModelName());
			alarmDeviceRulesEntity.setDeviceTypeId(x.getDeviceTypeId());
			alarmDeviceRulesEntity.setDeviceTypeName(x.getDeviceTypeName());
			return alarmDeviceRulesEntity;
		}).collect(Collectors.toList());
		this.saveBatch(alarmDeviceRulesList);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)

	@CacheEvict(value = CacheConstants.EQUIPMENT_ALARM_RULES,allEntries = true)
	public void addDeviceRulesByDevice(List<BasicDeviceEntity> deviceList, List<Long> rulesIds) {

		List<String> imeiList = deviceList.stream().map(BasicDeviceEntity::getImei).collect(Collectors.toList());
		//删除之前的记录
		remove(Wrappers.<AlarmDeviceRulesEntity>lambdaQuery().in(AlarmDeviceRulesEntity::getImei, imeiList));
		List<AlarmDeviceRulesEntity> alarmDeviceRulesList = new ArrayList<>();
		//新增新的
		deviceList.stream().forEach(x -> {
			List<AlarmDeviceRulesEntity> collect = rulesIds.stream().map(roleId -> {
				AlarmDeviceRulesEntity alarmDeviceRulesEntity = new AlarmDeviceRulesEntity();
				alarmDeviceRulesEntity.setAlarmRulesId(roleId);
				alarmDeviceRulesEntity.setImei(x.getImei());
				alarmDeviceRulesEntity.setDeviceModelId(x.getDeviceModelId());
				alarmDeviceRulesEntity.setDeviceModelName(x.getDeviceModelName());
				alarmDeviceRulesEntity.setDeviceTypeId(x.getDeviceTypeId());
				alarmDeviceRulesEntity.setDeviceTypeName(x.getDeviceTypeName());
				return alarmDeviceRulesEntity;
			}).collect(Collectors.toList());
			alarmDeviceRulesList.addAll(collect);
		});
		this.saveBatch(alarmDeviceRulesList);
	}



}